import { observable, action } from 'mobx'
import { getCurrentSeason } from "../../util/season/season";
import {axiosFetch, handleResp} from "../../util/common/http";
import {Toast} from "antd-mobile";
import history from "../../util/router/history";

export class UploadDishStore {
	@observable dishId: number|null = null;
	@observable season: number|null = getCurrentSeason();
	@observable oilyDegree: number = 0;
	@observable dishType: number = 1;
	@observable dishName: string|null = null;
	@observable dishPic: string|null = null;
	@observable dishDesc: string|null = null;
	@observable dishSource: string|null = null;

	// 菜品状态(1:可编辑 0:菜品不存在or出错了)
	@observable dishStatus: number = 1;
	// 提交按钮状态(1:禁用 2:可用 3:操作中)
	@observable submitBtnStatus: number = 2;

	/* 清空并初始化dish的信息 */
	@action clearDishInfo = () => {
		this.dishId = null;
		this.season = getCurrentSeason();
		this.oilyDegree = 0;
		this.dishType = 1;
		this.dishName = null;
		this.dishPic = null;
		this.dishDesc = null;
		this.dishSource = null;
	};

	@action initEditDishInfo = async (dishId: number) => {
		const dishInfoResp = await axiosFetch("/dish/query/one", {
			method: 'POST',
			data: {
				dishId: dishId
			}
		});
		if (handleResp(dishInfoResp, false)) {
			const {dishId, season, oilyDegree, dishType, dishName, dishPic, dishDesc, dishSource} = dishInfoResp.context;
			this.dishId = dishId;
			this.season = season;
			this.oilyDegree = oilyDegree;
			this.dishType = dishType;
			this.dishName = dishName;
			this.dishPic = dishPic;
			this.dishDesc = dishDesc;
			this.dishSource = dishSource;
		} else {
			this.dishStatus = 0;
		}
	};

	@action changeSeason = (value: number[]) => {
		this.season = value[0];
	};

	@action changeOilyDegree = (value: number[]) => {
		this.oilyDegree = value[0];
	};

	@action changeDishType = (value: number[]) => {
		this.dishType = value[0];
	};

	@action changeDishName = (value: string) => {
		this.dishName = value;
	};

	@action changeDishPic = async (value: string) => {
		// 若是null,则删除服务器上的物理存储的图片
		if (value == null) {
			const delResp = await axiosFetch("/dish/pic/delete", {
				method: 'DELETE',
				data: {
					dishPic: this.dishPic
				}
			});

			if (handleResp(delResp)) {
				this.dishPic = null;
			}
		} else {
			this.dishPic = value;
		}
	};

	@action changeDishDesc = (value: string) => {
		this.dishDesc = value;
	};

	@action changeDishSource = (value: string) => {
		this.dishSource = value;
	};

	@action onSubmit = async () => {
		if (!this.dishPic) {
			Toast.fail("菜品图片必须上传!", 1.5);
			return false;
		}
		if (!this.dishName) {
			Toast.fail("菜品名称必填!", 1.5);
			return false;
		}
		if (!this.dishDesc) {
			Toast.fail("菜品简介必填!", 1.5);
			return false;
		}
		this.submitBtnStatus = 3;
		let fetchResp;
		const params = {
			season: this.season,
			oilyDegree: this.oilyDegree,
			dishType: this.dishType,
			dishName: this.dishName,
			dishPic: this.dishPic,
			dishDesc: this.dishDesc,
			dishSource: this.dishSource,
		};

		if (this.dishId == null) {
			// 添加
			fetchResp = await axiosFetch("/dish/add", {
				method: 'POST',
				data: params
			});
		} else {
			// 编辑
			fetchResp = await axiosFetch("/dish/update", {
				method: 'PUT',
				data: {
					dishId: this.dishId,
					...params
				}
			});
		}

		this.submitBtnStatus = 2;
		if (handleResp(fetchResp)) {
			history.goBack();
		}
	}
}

export default new UploadDishStore();
